package command

import "errors"

type tMockCanvas struct {
	commands []IDrawCommand
	graphics IGraphics
}

func NewMockCanvas() ICanvas {
	return &tMockCanvas{
		commands: make([]IDrawCommand, 0),
		graphics: newMockGraphics(),
	}
}

func (me *tMockCanvas) Command(cmd IDrawCommand) {
	me.push(cmd)
	me.update()
}

func (me *tMockCanvas) Undo() {
	e,_ := me.pop()
	if e != nil {
		return
	}

	me.update()
}

func (me *tMockCanvas) update() {
	me.graphics.Clear()
	for _,it := range me.commands {
		it.Draw(me.graphics)
	}
}


func (me *tMockCanvas) push(cmd IDrawCommand) {
	me.commands = append(me.commands, cmd)
}

func (me *tMockCanvas) pop() (error, IDrawCommand) {
	size := len(me.commands)
	if size <= 0 {
		return errors.New("no more commands"), nil
	}

	it := me.commands[size - 1]
	me.commands = me.commands[:size - 1]
	return nil,it
}



